# Contributor: Pablo Castorino <pcastorino@mendoza-conicet.gob.ar>
# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=keepalived
pkgver=2.2.8
pkgrel=0
pkgdesc="Health Checking for Linux Virtual Server & High-Availability"
url="https://www.keepalived.org/"
arch="all"
license="GPL-2.0-only"
depends="$pkgname-common"
makedepends="
	autoconf
	automake
	libnfnetlink-dev
	libnl3-dev
	linux-headers
	net-snmp-dev
	openssl-dev>3
	popt-dev
	"
install="$pkgname.post-upgrade"
subpackages="
	$pkgname-doc
	$pkgname-sample-config:samples:noarch
	$pkgname-openrc
	$pkgname-common
	$pkgname-snmp
	"
source="https://www.keepalived.org/software/keepalived-$pkgver.tar.gz
	always-use-pid-dir.patch
	$pkgname.initd
	$pkgname.confd
	"

# secfixes:
#   2.2.7-r0:
#     - CVE-2021-44225
#   2.0.11-r0:
#     - CVE-2018-19044
#     - CVE-2018-19045
#     - CVE-2018-19046

prepare() {
	default_prepare

	./autogen.sh
	cp -ar "$builddir" "$builddir-snmp"
}

build() {
	cd "$builddir-snmp"
	_build --enable-snmp --enable-snmp-rfc

	cd "$builddir"
	_build
}

_build() {
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--localstatedir=/var \
		--enable-vrrp \
		--enable-sha1 \
		"$@"
	make
}

check() {
	"$builddir"/bin/keepalived --version
	"$builddir-snmp"/bin/keepalived --version
}

package() {
	provider_priority=100  # highest (other provider is keepalived-snmp)

	make DESTDIR="$pkgdir" install

	local f; for f in AUTHOR CONTRIBUTORS ChangeLog README INSTALL TODO; do
		install -m644 -D $f "$pkgdir"/usr/share/doc/$pkgname/$f
	done

	install -m755 -D "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
	install -m644 -D "$srcdir"/$pkgname.confd "$pkgdir"/etc/conf.d/$pkgname

	# Clean up sysvinit files
	rm -rf "$pkgdir"/etc/rc.d "$pkgdir"/etc/sysconfig
}

samples() {
	pkgdesc="Sample configuration for keepalived"
	depends=""

	amove etc/keepalived/
}

openrc() {
	default_openrc
	install_if="openrc $pkgname-common=$pkgver-r$pkgrel"
}

# This is used only to install keepalived-openrc for both keepalived and
# keepalived-snmp (direct methods didn't work, dunno why).
common() {
	depends=""
	mkdir -p "$subpkgdir"
}

snmp() {
	pkgdesc="$pkgdesc (with SNMP support)"
	depends="$pkgname-common"
	provides="$pkgname=$pkgver-r$pkgrel"
	provider_priority=10  # lowest (other provider is keepalived)

	cd "$builddir-snmp"

	install -m700 -D bin/keepalived "$subpkgdir"/usr/sbin/keepalived
	mkdir "$subpkgdir"/usr/bin
	ln -s ../sbin/keepalived "$subpkgdir"/usr/bin/genhash

	mkdir -p "$subpkgdir"/usr/share/snmp/mibs
	install -m644 doc/*-MIB.txt "$subpkgdir"/usr/share/snmp/mibs/
}

sha512sums="
dc0ab5b0ef8911a7859422eccc2771a40e942236c855a628158ed748eb5f7dc4b6f4850e9c3057e81fd9d2daa640ab51fb1d7af12748a613280a217b333eb06b  keepalived-2.2.8.tar.gz
40c0847b5f13852cac5a9bfef51f885b0f4e2685fca571d768e0f71ca2555be3ef6c53a3a0efa434b105e079144997a21dbaa9a6ca0f649ea8f0209ac9a3f17e  always-use-pid-dir.patch
4f1a3fdb0a936707a234efa063296f575fa8168551c87d2f9d43b7681d24605f852b4ddb2c674d1e311111f85232a708939a71cf128eb57c940ccb6927518dd4  keepalived.initd
2e6a9411d78fb954b5ede75d04225bf3f37d9ef4773c2b3587e1de1a0795f61a1ed784fa229854c2ee97f153205c3f76b3ff79cd55ede07f211731f0d17569cb  keepalived.confd
"
